<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .banner {
            width: 500px;
            height: 300px;
            margin: 50px auto;
            position: relative;
        }

        .banner ul li {
            width: 500px;
            height: 300px;
            position: absolute;
            top: 0;
            left: 0;
            background-color: #f00;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 50px;

            opacity: 0;
            transition: .3s;

            pointer-events: none;
        }

        .banner span {
            position: absolute;
            top: 0;
            bottom: 0;
            margin: auto;
            width: 30px;
            height: 30px;
            background-color: rgba(0, 0, 0, .5);
            color: #fff;
            text-align: center;
            line-height: 30px;
        }

        .next {
            right: 0;
        }

        ol {
            position: absolute;
            width: 100%;
            display: flex;
            justify-content: center;
            bottom: 0;
        }

        ol li {
            width: 20px;
            height: 20px;
            margin: 10px;
            background-color: #666;
        }

        .active {
            background-color: #ff0;
        }

        .show {
            opacity: 1 !important;
        }
    </style>
</head>

<body>


    <div class="banner">
        <ul class="list">
            <li class="show">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
        <ol>
            <li class="item active"></li>
            <li class="item"></li>
            <li class="item"></li>
            <li class="item"></li>
        </ol>
        <span class="prev"></span>
        <span class="next"></span>
    </div>


    <script>



        function Banner(ele) {
            this.oBanner = document.querySelector(ele);
            this.oImgs = this.oBanner.querySelectorAll('ul li');
            this.oDots = this.oBanner.querySelectorAll('ol li');
            this.oPrev = this.oBanner.querySelector('.prev');
            this.oNext = this.oBanner.querySelector('.next');

            // 
            this.index = 0;
            // 
            this.timer = null;

            // 
            this.autoPlay();

            // 绑定的一些事件
            this.event();
        }


        // 自动播放
        Banner.prototype.autoPlay = function () {
            this.timer = setInterval(() => {
                this.index++;
                if (this.index === this.oImgs.length) {
                    this.index = 0;
                }
                // 显示这张图片
                this.show();
            }, 2000)
        }

        // 显示某一张图片
        Banner.prototype.show = function () {
            // 清除所有
            this.oImgs.forEach((v, i) => {
                v.classList.remove('show');
                this.oDots[i].classList.remove('active');
            })
            // 显示当前这一张
            this.oImgs[this.index].classList.add('show');
            this.oDots[this.index].classList.add('active');
        }


        Banner.prototype.event = function () {

            this.oDots.forEach((v, i) => {
                v.aa = i;
            })

            this.oBanner.onclick = e => {
                const target = e.target;
                // 上翻页
                if (target.className === 'prev') {
                    clearInterval(this.timer);
                    this.index--;
                    if (this.index === -1) {
                        this.index = this.oImgs.length - 1;
                    }
                    this.show();
                    return
                }
                // 下翻页
                if (target.className === 'next') {
                    clearInterval(this.timer);
                    this.index++;
                    if (this.index === this.oImgs.length) {
                        this.index = 0;
                    }
                    this.show();
                }
                // 小点切换
                if (target.tagName === 'LI') {
                    // if (target.className === 'item') {
                    clearInterval(this.timer);
                    this.index = target.aa;
                    this.show()
                }
            }


            // 鼠标移入移出

        }



        new Banner('.banner');

    </script>

</body>

</html>